package cn.lbd;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CuratorCreate {
    CuratorFramework client;
    @Before
    public void init() {
        client = CuratorFrameworkFactory.builder()
                .connectString("39.97.191.1:2181")
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .namespace("create_new")
                .build();

        client.start();
        System.out.println(client.isStarted());
    }

    @After
    public void destroy() {
        client.close();
    }

    //使用内置的常量权限
    @Test
    public void test1() throws Exception{
        client.create()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/demo1","demo1".getBytes());
        System.out.println("结束");
    }

    //使用自定义的权限列表
    @Test
    public void test2() throws Exception{
        List<ACL> acls = new ArrayList<>();
        Id id = new Id("world","anyone");
        acls.add(new ACL(ZooDefs.Perms.ALL,id));
        client.create()
                .withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                .withACL(acls)
                .forPath("/demo2","demo2".getBytes());
        System.out.println("结束");
    }

    //递归创建结点数
    @Test
    public void test3() throws Exception{
        client.create()
                //递归节点的创建，如果没有创建demo10节点，不加上这一项配置会报错，加上了就会递归的先创建demo10，在创建ddemo10
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/demo10/ddemo10","demo10".getBytes());
        System.out.println("结束");
    }

    //异步创建节点1
    @Test
    public void test4() throws Exception{
        client.create()
                //递归节点的创建，如果没有创建demo10节点，不加上这一项配置会报错，加上了就会递归的先创建demo10，在创建ddemo10
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                //异步回调接口
                .inBackground(new BackgroundCallback() {
                    @Override
                    public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                        //我们指定的节点路径，如果是有序节点的创建方式，真实名称会发生改变
                        System.out.println(curatorEvent.getPath());
                        //存在在Zookeeper服务器的真实节点名
                        System.out.println(curatorEvent.getName());
                        //事件类型
                        System.out.println(curatorEvent.getType());
                    }
                })
                .forPath("/async/async_child2","async".getBytes());
        TimeUnit.SECONDS.sleep(5);
        System.out.println("结束");
    }
}
